var util = require('../../utils/util.js')
const app = getApp()
let pagesize = 20
let newsShareMinImg = 'https://6d61-main-9g11cspaf548a298-1306430077.tcb.qcloud.la/cloudbase-cms/upload/2021-09-29/laow2xw45c6ml5imqgbpdb9nc8s8fgk1_.gif'
let newsShareText = '这个笑话真有意思，进来看看吧'
let baseUrl = 'https://v.juhe.cn/joke/content/list.php'
let shareid = null
let key = 'bbee57a486ce767fa159d86753b8f3a9'
var today = Math.round(new Date() / 1000)
var chazhi = 315360000
var time = today - chazhi

Page({
  data: {
    fangwencishu: 0,
    fangwenrenshu: 0,
    // 组件
    TabCur: 0,
    jockList: []
  },

  // 复制
  OnClick(e) {

    console.log('点击了复制按钮')
    console.log('接受到参数:',e.currentTarget.dataset.value)
    
    wx.setClipboardData({
      data: e.currentTarget.dataset.value,
      success: function (res) {
        console.log("复制成功:", res)
      },
    })

  },
  /**
   * 用户点击右上角分享
   */
  onShareAppMessage(e) {
   
    return {
      title:'',
      imageUrl:'',
      path: `/pages/news/news`
    }
  },
  onShareTimeline(e) {

      return {
        title: newsShareText,
        imageUrl: newsShareMinImg
      }
    
  },
    /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {
    console.log("上拉");
    let page = this.data.jockList.length/pagesize +1
    
    this.getJockList(page,true)
      },
  getJockList(page = 1,tag=false) {
    if(!tag){
      try {
        var value = wx.getStorageSync('jockList')
        if (value) {
          // Do something with return value
          console.log("本地有数据", value);
          if (today - value.time < 3600 * 24) {
            // 返回本地存储的数据
            this.setData({
              jockList:value.jockLiost
            })
            return
          } 
        }
      } catch (e) {
        // Do something when catch error
        console.log("本地没有数据", e);
      }
    }

   

    wx.request({
      url: `${baseUrl}?key=${key}&page=${page}&pagesize=${pagesize}&sort=asc&time=${time}`,
      success: res => {
        if (res.data.result == null) {
          wx.showToast({
            icon: 'none',
            title: '系统错误',
          })
          return
        }
        if (res.data.result.data.length == 0) {
          wx.showToast({
            icon: 'none',
            title: '没有数据了',
          })
          return
        }
        this.setData({
          jockList: this.data.jockList.concat(res.data.result.data)
        })
        wx.setStorage({
          key: "jockList",
          data: {
            jockLiost: this.data.jockList,
            time: today
          }
        })
      },
      fail: res => {
        console.log(res, '请求笑话数据出错，请求失败');
      }
    })


  },
  onLoad: function () {

    this.getJockList()
   
    app.selecteUser().then(res => {
      if (res == false) {
        return
      }
      userInfo = res
      if (res == 'Not_registered') {
        this.getUserProfileModel()
      }
    })
  },
  //  返回顶部功能
  //返回顶部
  goTop: function (e) {
    if (wx.pageScrollTo) {
      wx.pageScrollTo({
        scrollTop: 0
      })
    } else {
      wx.showModal({
        title: '提示',
        content: '当前微信版本过低，无法使用该功能，请升级到最新微信版本后重试。'
      })
    }
  },

  onShow() {

  },
  // 授权弹窗
  getUserProfileModel() {
    // 获取授权
    wx.showModal({
      title: '提示',
      content: '授权后才可以继续操作',
      complete: res => {
        if (res.confirm) {
          this.getUserProfile()
        }
      }
    })
  },
  // 获取登录授权
  getUserProfile() {
    wx.getUserProfile({
      desc: '用于完善资料', // 声明获取用户个人信息后的用途，后续会展示在弹窗中，请谨慎填写
      success: (res) => {
        app.globalData.userInfo = res.userInfo
        this.zhuceUser(app.globalData.userInfo)
      },
      fail: () => {}
    })
  },
  // 注册用户
  zhuceUser(userInfo) {
    wx.cloud.callFunction({
        name: 'user',
        data: {
          tag: 'zhuceUser',
          avatarUrl: userInfo.avatarUrl,
          city: userInfo.city,
          gender: userInfo.gender,
          nickName: userInfo.nickName,
          province: userInfo.province,
          createTime: util.formatTime(new Date()),
          _updateTime: util.formatTime(new Date()).slice(0, 10)
        }
      })
      .then(res => {
        wx.showToast({
          icon: 'none',
          title: "☺"
        })
      })
  },


})